/*
 * Copyright© 2003-2019 浙江汇信科技有限公司, All Rights Reserved.
 */
package com.icinfo.cloud.provider.punish.config.service.impl;

import com.icinfo.cloud.provider.common.exception.BusinessException;
import com.icinfo.cloud.provider.punish.config.dto.ConfigPaymentChannelItemDetailQueryDto;
import com.icinfo.cloud.provider.punish.config.mapper.ConfigPaymentChannelItemDetailMapper;
import com.icinfo.cloud.provider.punish.config.model.ConfigPaymentChannelItemDetail;
import com.icinfo.cloud.provider.punish.config.service.IConfigPaymentChannelItemDetailService;
import com.icinfo.cloud.provider.punish.config.vo.ConfigPaymentChannelItemDetailVo;
import com.icinfo.framework.common.web.Result;
import com.icinfo.framework.core.service.support.MyBatisServiceSupport;
import com.icinfo.framework.mybatis.mapper.entity.Example;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * 描述: 缴费渠道关联项目明细配置表 config_payment_channel_item_detail 对应的Service接口实现类.<br>
 *
 * @author framework generator
 * @date 2022年03月01日
 */
@Service
@Slf4j
public class ConfigPaymentChannelItemDetailServiceImpl extends MyBatisServiceSupport implements IConfigPaymentChannelItemDetailService {
    @Resource
    private ConfigPaymentChannelItemDetailMapper configPaymentChannelItemDetailMapper;

    /**
     * 描述：查询缴费渠道关联项目明细配置信息
     *
     * @param queryDto {@link ConfigPaymentChannelItemDetailQueryDto}
     * @return {@link List <ConfigPaymentChannelItemDetailVo>} 缴费渠道关联项目明细配置信息
     * @author xiangyuyu
     * @date 2022-03-01 16:51
     */
    @Override
    public List<ConfigPaymentChannelItemDetailVo> getConfigPaymentChannelItemDetail(ConfigPaymentChannelItemDetailQueryDto queryDto) {
        String paymentChannelId = queryDto.getPaymentChannelId();
        if (ObjectUtils.isEmpty(paymentChannelId)) {
            throw new BusinessException("未指定缴费渠道id");
        }
        List<ConfigPaymentChannelItemDetailVo> configPaymentChannelItemDetailList
                = configPaymentChannelItemDetailMapper.selectPaymentChannelConfigInfo(queryDto);
        return configPaymentChannelItemDetailList;
    }

    /**
     * 描述：保存缴费渠道关联项目明细配置信息
     *
     * @param configPaymentChannelItemDetail {@link ConfigPaymentChannelItemDetail}
     * @return {@link Result <String>} 保存结果
     * @author xiangyuyu
     * @date 2022-03-01 16:51
     */
    @Override
    public Result<String> saveConfigPaymentChannelItemDetail(ConfigPaymentChannelItemDetail configPaymentChannelItemDetail) {
        ConfigPaymentChannelItemDetailQueryDto queryDto = new ConfigPaymentChannelItemDetailQueryDto();
        String paymentChannelId = configPaymentChannelItemDetail.getPaymentChannelId();
        String chitCode = configPaymentChannelItemDetail.getChitCode();

        if (ObjectUtils.isEmpty(paymentChannelId)) {
            throw new BusinessException("未指定缴费渠道id");
        }
        if (ObjectUtils.isEmpty(chitCode)) {
            throw new BusinessException("未指定执收项目编码");
        }
        queryDto.setPaymentChannelId(paymentChannelId);
        queryDto.setChitCode(chitCode);
        //根据唯一索引查询记录数
        List<ConfigPaymentChannelItemDetailVo> configPaymentChannelItemDetailList = getConfigPaymentChannelItemDetail(queryDto);
        if (ObjectUtils.isEmpty(configPaymentChannelItemDetailList) || configPaymentChannelItemDetailList.size() == 0) {
            //I
            configPaymentChannelItemDetail.setOpType("I");
        } else {
            //U(暂不考虑删除)
            Example updateExample = new Example(ConfigPaymentChannelItemDetail.class);
            updateExample.createCriteria().andEqualTo("paymentChannelId", paymentChannelId)
                    .andEqualTo("chitCode", chitCode);
            configPaymentChannelItemDetail.setOpType("U");
            configPaymentChannelItemDetailMapper.updateByExampleSelective(configPaymentChannelItemDetail, updateExample);
        }
        return Result.success("保存成功");
    }
}